package de.bht.esa.weather.usermanager;

import java.io.Serializable;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Andreas Goese
 * @author Sven Höche
 * 
 * Diese Klasse representiert die Entität eines Benutzers. * 
 */

@XmlRootElement
@Entity
public class FrontendUser implements Serializable {
    
    private static final long serialVersionUID = 13453959374598L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(unique=true)
    private String name; //Muss unique sein!!
    private String password; //Muss MD5 gehasht sein!
    private String email;
    private String userRole; //Siehe das enum oben!
    private List<UserResource> resources;
    private int requestQuota; //Wie viele requests der User noch machen darf

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    /**
     * Setst ein gehashtes Passwort für den Benutzer. 
     * @param password zue setzendes Passwort des Benutzers
     */
    public void setPasswordMD5(String password){
        MessageDigest digest; 
        String md5;
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException ex) {
            throw new RuntimeException("MD5-Generation failed", ex);
        }
        digest.update(password.getBytes(), 0 , password.length());
        md5 = new BigInteger(1, digest.digest()).toString(16);
        this.password = md5;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getUserRole() {
        return userRole;
    }

    public void setUserRole(String userRole) {
        this.userRole = userRole;
    }

    public List<UserResource> getResources() {
        return resources;
    }

    public void setResources(List<UserResource> resources) {
        this.resources = resources;
    }

    public int getRequestQuota() {
        return requestQuota;
    }

    public void setRequestQuota(int requestQuota) {
        this.requestQuota = requestQuota;
    }
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof FrontendUser)) {
            return false;
        }
        FrontendUser other = (FrontendUser) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "de.bht.esa.weather.frontend.usermanager.FrontendUser[ id=" + id + " ]";
    }
}